package cn.tedu.csmall.product.controller;

import cn.tedu.csmall.product.pojo.dto.AlbumAddNewDTO;
import cn.tedu.csmall.product.pojo.dto.AlbumUpdateDTO;
import cn.tedu.csmall.product.pojo.vo.AlbumListItemVO;
import cn.tedu.csmall.product.pojo.vo.AlbumStandardVO;
import cn.tedu.csmall.product.service.AlbumService;
import cn.tedu.csmall.product.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 处理相册相关请求的控制器
 *
 * @author Chen
 */
@Slf4j
@RestController
@RequestMapping("/albums")
@Tag(name = "04. 相册管理模块")
@Validated
public class AlbumController {

    private final AlbumService albumService;

    @Autowired
    public AlbumController(AlbumService albumService) {
        log.debug("创建控制器对象：AlbumController");
        this.albumService = albumService;
    }

    // http://localhost:8088/albums/add-new
    @ApiOperationSupport(order = 100)
    @Operation(summary = "添加相册")
    @PostMapping("/add-new")
    public JsonResult<Void> addNew(@Valid AlbumAddNewDTO albumAddNewDTO) {
        log.debug("开始处理【添加相册】的请求，参数：{}", albumAddNewDTO);
        albumService.addNew(albumAddNewDTO);
        return JsonResult.ok();
    }

    // http://localhost:8088/albums/9527/delete
    @Parameter(name = "id", description = "相册ID", required = true, example = "9527",
            schema = @Schema(type = "integer", format = "int64"))
    @ApiOperationSupport(order = 200)
    @Operation(summary = "根据id删除相册")
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult<Void> delete(@Range(min = 1L, max = 10000L, message = "删除相册失败，相册ID非法！") @PathVariable Long id) {
        log.debug("开始处理【根据id删除相册】的请求，参数：{}", id);
        albumService.delete(id);
        return JsonResult.ok();
    }

    // http://localhost:8088/albums/9527/update
    @ApiOperationSupport(order = 300)
    @Operation(summary = "根据id修改相册")
    @PostMapping("/{id:[0-9]+}/update")
    public JsonResult<Void> update(@PathVariable Long id, AlbumUpdateDTO albumUpdateDTO) {
        log.debug("开始处理【根据id修改相册详情】的请求，参数ID：{}，新数据：{}", id, albumUpdateDTO);
        albumService.updateInfoById(id, albumUpdateDTO);
        return JsonResult.ok();
    }

    // http://localhost:8088/albums/9527
    @Parameter(name = "id", description = "相册ID", required = true, example = "9527",
            schema = @Schema(type = "integer", format = "int64"))
    @ApiOperationSupport(order = 410)
    @Operation(summary = "根据id查询相册详情")
    @GetMapping("/{id:[0-9]+}")
    public JsonResult<AlbumStandardVO> getStandardById(@Range(min = 1L, max = 10000L, message = "查询相册详情失败，相册ID非法！")
                                                       @PathVariable Long id) {
        log.debug("开始处理【根据id查询相册详情】的请求，参数：{}", id);
        AlbumStandardVO album = albumService.getStandardById(id);
        return JsonResult.ok(album);
    }

    // http://localhost:8088/albums
    @ApiOperationSupport(order = 420)
    @Operation(summary = "查询相册列表")
    @GetMapping("")
    public JsonResult<List<AlbumListItemVO>> list() {
        log.debug("开始处理【查询相册列表】的业务，无参数");
        List<AlbumListItemVO> list = albumService.list();
        return JsonResult.ok(list);
    }

}
